package com.ys.peony.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.yklab.library.refreshlayout.adapters.NewCommonRecyclerViewAdapter;
import com.yklab.library.refreshlayout.holder.CommonRecyclerViewHolder;
import com.yklab.library.refreshlayout.inter.OnClickCommonListener;
import com.yklab.library.ui.adapter.LoadMoreWrapper;
import com.ys.peony.R;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.enums.BusinessDetailStatus;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.JiekuanrenModel;
import com.ys.peony.model.JiekuanrenResp;
import com.ys.peony.model.UserModel;
import com.ys.peony.ui.widget.Divider;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 补件列表
 * Created by yml on 17/5/12.
 */

public class PatchListActivity extends ListActivity<JiekuanrenModel> implements OnClickCommonListener {

    @Override
    protected int getContentResId() {
        return super.getContentResId();
    }

    @Override
    public void initView() {
        setShowToolbar(true);
        super.initView();
        toolbar.setTitle("补件列表");
        initAdapter();
        //数据请求
        refreshlayout.beginRefreshing();
    }

    private void initAdapter() {
        mAdapter = new NewCommonRecyclerViewAdapter<JiekuanrenModel>(this, mList, R.layout.patch_item_layout) {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                JiekuanrenModel model = mList.get(position);
                if (model == null) {
                    return;
                }
                String patchStatus = "";
                if (TextUtils.isEmpty(model.getPatch_status())) {
                    patchStatus = "未补件";
                } else if ("0".equals(model.getPatch_status())) {
                    patchStatus = "待提交";
                } else if ("1".equals(model.getPatch_status())) {
                    patchStatus = "已提交";
                } else {
                    patchStatus = BusinessDetailStatus.getStatus(model.getLoanStatus());
                }
                ((CommonRecyclerViewHolder) holder).setImgResource(R.id.jiekuanren_item_icon, R.mipmap.repair)
                        .setText(R.id.jiekuanren_name_tv, BaseApplication.getInstance().getResources().getString(R.string.name_lable) + model.getClientName())
                        .setText(R.id.jiekuanren_kehu_tv,BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_kehu) + model.getClientClassfyName())
                        .setText(R.id.jiekuanren_status_tv, "补件状态：" + patchStatus)
                        .setText(R.id.jiekuanren_yewunum_tv, BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_yewu_num) + model.getSerialNumber());

                if (model.getVehicleList() != null && model.getVehicleList().size() > 0) {
                    ((CommonRecyclerViewHolder) holder).setText(R.id.jiekuanren_car_tv, BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + model.getVehicleList().get(0).getBrand_name())
                            .setText(R.id.jiekuanren_carnum_tv,BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + model.getVehicleList().get(0).getPlate_number());
                } else {
                    ((CommonRecyclerViewHolder) holder).setText(R.id.jiekuanren_car_tv, BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + "无")
                            .setText(R.id.jiekuanren_carnum_tv, BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + "无");
                }
            }
        };
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mAdapter.setOnClickListener(this);
        mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
        recyclerview.setHasFixedSize(true);
        recyclerview.addItemDecoration(new Divider(this));
        recyclerview.setLayoutManager(mLayoutManager);
        recyclerview.setAdapter(mLoadMoreWrapper);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (YSConstant.ACTIVITY_ADD_YW_CLIENT == requestCode && resultCode == RESULT_OK) {
            refreshlayout.beginRefreshing();
        }
    }

    @Override
    protected void getData() {
        UserModel userModel = SharePreferencesUtils.getInstance().getUserModel();
        if (null == userModel) {
            ToastUtils.showToast("用户信息为空");
            return;
        }
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(userModel.getId()));
        params.put("currentPage", curPageNum);
        params.put("pageNum", YSConstant.PAGE_NUM);
        httpManager.get(getUrl(), params, new BaseRespCallBack() {

            @Override
            public void onRespSuccess(String respStr) {
                refreshlayout.endRefreshing();
                setData(respStr);
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                refreshlayout.endRefreshing();
                showBlankView();
            }
        });
    }

    private void setData(final String data) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(data)) {
                    return;
                }
                JiekuanrenResp resp = JSONUtil.parseObject(data, JiekuanrenResp.class);
                if (actionType == YSConstant.ACTION_TYPE_DOWN && (resp == null || resp.getDataList() == null || resp.getDataList().size() < 1)) {
                    showBlankView();
                    return;
                }
                List<JiekuanrenModel> list = resp.getDataList();
                if ((list == null || list.size() < 1) && actionType == YSConstant.ACTION_TYPE_UP) {
                    mLoadMoreWrapper.setLoadState(LoadMoreWrapper.LOADING_END);
                    return;
                }
                if (actionType == YSConstant.ACTION_TYPE_DOWN) {
                    hideBlankView();
                    mList.clear();
                    mRequestList.clear();
                    mList.addAll(list);
                    mRequestList.addAll(list);
                    mLoadMoreWrapper.notifyDataSetChanged();
                } else if (actionType == YSConstant.ACTION_TYPE_UP) {
                    mList.addAll(list);
                    mRequestList.addAll(list);
                    mLoadMoreWrapper.notifyItemRangeInserted(mList.size() - list.size() + 1, list.size());
                }
            }
        });
    }

    @Override
    protected String getUrl() {
        return HttpURL.patchLoanPage;
    }

    @Override
    public void observerSearchText(List list, String text) {
        mList.clear();
        for (int i = 0; i < list.size(); i++) {
            JiekuanrenModel jiekuanrenModel = (JiekuanrenModel) list.get(i);
            if (jiekuanrenModel.getSerialNumber().contains(text) || jiekuanrenModel.getClientName().contains(text)) {
                mList.add(jiekuanrenModel);
            }
        }
        mLoadMoreWrapper.notifyDataSetChanged();
    }

    @Override
    protected void searchData(String respStr) {
        final JiekuanrenResp resp = JSONUtil.parseObject(respStr, JiekuanrenResp.class);
        if (resp != null && resp.getDataList() != null) {
            mList.clear();
            mList.addAll(resp.getDataList());
            mLoadMoreWrapper.notifyDataSetChanged();
        }
    }

    @Override
    public void onClickListener(View view, int position) {
        JiekuanrenModel jiekuanrenModel = mList.get(position);
        //图片编辑
        Intent it = new Intent(this, BusiLoanInfoActivity.class);
        try {
            it.putExtra("loanModel", jiekuanrenModel);
            it.putExtra("fileType", "2");
        } catch (Exception e) {

        }
        startActivityForResult(it, YSConstant.ACTIVITY_ADD_YW_CLIENT);
    }
}
